//
//  DHOrderProductView.m
//  Dresshere
//
//  Created by ZhangPeng on 2018/1/21.
//  Copyright © 2018年 张朋. All rights reserved.
//

#import "DHOrderProductView.h"
#import "DHOrder_LeasedCell.h"
#import "DHOrderPayViewController.h"
#import "DHOrderDetail_CheckViewController.h"

@interface DHOrderProductView()<UITableViewDelegate, UITableViewDataSource>

@property (strong, nonatomic) UITableView *tableView;
@property (strong, nonatomic) NSMutableArray *ordersArray;
@property (assign, nonatomic) NSInteger pageIndex;
@property (strong, nonatomic) UILabel *noDataLabel;

@end

@implementation DHOrderProductView

- (NSMutableArray *)ordersArray{
    if (!_ordersArray) {
        _ordersArray = [NSMutableArray array];
    }
    return _ordersArray;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        if (!self.tableView) {
            self.tableView = [[UITableView alloc] init];
            self.tableView.delegate = self;
            self.tableView.dataSource = self;
            [self.tableView registerNib:[UINib nibWithNibName:@"DHOrder_LeasedCell" bundle:nil] forCellReuseIdentifier:kDHOrder_LeasedCell];
            [self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
            [self addSubview:self.tableView];
            self.tableView.mj_header = [DHMJRefreshGifHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadOrders)];
            self.tableView.mj_footer = [DHMJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreOrders)];
        }
        if (!self.noDataLabel) {
            self.noDataLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, 50)];
            self.noDataLabel.textAlignment = NSTextAlignmentCenter;
            self.noDataLabel.font = [UIFont systemFontOfSize:16];
            self.noDataLabel.textColor = [UIColor colorWithHexString:@"#4a4a4a"];
            self.noDataLabel.text = @"暂时没有订单";
            [self addSubview:self.noDataLabel];
        }
        self.noDataLabel.hidden = YES;
    }
    return self;
}

- (void)layoutSubviews{
    [super layoutSubviews];
    [self loadOrders];
    
    self.tableView.frame = CGRectMake(0, 0, self.width, self.height);
}

- (void)loadOrders{
    self.pageIndex = 0;
    [self.tableView.mj_footer resetNoMoreData];
    [[DHNetAPIClient shareDHNetAPIClient] request_ordersWithParams:@{@"pageIndex":@(self.pageIndex),@"pageSize":@(20)} andBlock:^(id data, NSError *error) {
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        [self.ordersArray removeAllObjects];
        [self.ordersArray addObjectsFromArray:[data objectForKey:@"orders"]];
        [self.tableView reloadData];
        if (self.ordersArray.count < 20) {
            self.tableView.mj_footer.hidden = YES;
        }else{
            self.tableView.mj_footer.hidden = NO;
        }
        if (self.ordersArray.count == 0) {
            self.noDataLabel.hidden = NO;
            self.noDataLabel.text = @"暂时没有订单";
        }else{
            self.noDataLabel.hidden = YES;
        }
        self.pageIndex += 1;
    }];
}

- (void)loadMoreOrders{
    [[DHNetAPIClient shareDHNetAPIClient] request_ordersWithParams:@{@"pageIndex":@(self.pageIndex),@"pageSize":@(20)} andBlock:^(id data, NSError *error) {
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        [self.ordersArray addObjectsFromArray:[data objectForKey:@"orders"]];
        [self.tableView reloadData];
        if ([[data objectForKey:@"orders"] count] < 20) {
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
        }
    }];
}

#pragma mark - UITableViewDataSource and UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.ordersArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *orderDic = [self.ordersArray objectAtIndex:indexPath.row];
    if ([[orderDic objectForKey:@"status"] integerValue] == OrderStateUnusual || [[orderDic objectForKey:@"status"] integerValue] == OrderStateCancel) {
        return 120 + 100 * [[orderDic objectForKey:@"content"] count];
    }else{
        return 120 + 100 * [[orderDic objectForKey:@"content"] count];
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    DHOrder_LeasedCell *cell = [tableView dequeueReusableCellWithIdentifier:kDHOrder_LeasedCell forIndexPath:indexPath];
    NSDictionary *orderDic = [self.ordersArray objectAtIndex:indexPath.row];
    [cell updateCell:orderDic];
    cell.tableView.tag = indexPath.row;
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *orderDic = [self.ordersArray objectAtIndex:indexPath.row];
    DHOrderDetail_CheckViewController *odController = [[DHOrderDetail_CheckViewController alloc] init];
    odController.orderID = [orderDic objectForKey:@"_id"];
    [self.viewController.navigationController pushViewController:odController animated:YES];
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    self.noDataLabel.hidden = YES;
}


@end
